import 'dart:async';

import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///编写引导页的主体

class Guidance extends StatefulWidget {
  @override
  _GuidanceState createState() => _GuidanceState();
}

class _GuidanceState extends State<Guidance> {
  int _date = 1;
  int _countdown = 5;
  DateTime _dateTime = DateTime.now();
  Timer _countdownTimer;
  List _poem = [
    [
      "\n回\n家\n篇\n",
      "\n月\n是\n故\n乡\n明\n，\n情\n是\n进\n乡\n怯",
      "\n游\n子\n环\n四\n海\n，\n月\n圆\n切\n回\n家"
    ],
    ["\n团\n圆\n饭\n篇\n",
      "\n千\n里\n奔\n袭\n，\n只\n为\n团\n圆\n",
      "\n中\n秋\n佳\n节\n，\n你\n我\n共\n庆\n"
    ],
    [
      "\n赏\n月\n篇",
      "\n一\n起\n拉\n家\n常\n，\n笑\n谈\n人\n生\n事\n",
      "\n一\n家\n人\n饭\n后\n，\n端\n坐\n圆\n月\n下\n"
    ],
    [
    "\n晚\n安\n篇\n",
    "\n一\n年\n一\n次\n，\n虽\n然\n短\n暂\n",
    "\n亲\n情\n相\n融\n，\n我\n们\n下\n次\n再\n聚\n"
  ]

  ];

  @override
  void initState() {
    super.initState();
    if (_dateTime.day <= 19) {
      ///19号之前,人们都在回家的路上
      _date = 1;
    } else if (_dateTime.day == 20) {
      ///20号，人们回到家中，吃上团圆饭
      _date = 2;
    } else if (_dateTime.day == 21) {
      ///21号，中秋快乐
      _date = 3;
    } else {
      ///中秋过后，亲人回到忙碌的生活，期盼着下一次团聚
      _date = 4;
    }
    _startRecordTime();
  }

  @override
  void dispose() {
    super.dispose();
    print('启动页面结束');
    if (_countdownTimer != null && _countdownTimer.isActive) {
      _countdownTimer.cancel();
      _countdownTimer = null;
    }
  }

  void _startRecordTime() {
    _countdownTimer = Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        if (_countdown <= 1) {
          ///此处编写你需要跳转的界面
          // _countdownTimer.cancel();
          // _countdownTimer = null;
        } else {
          _countdown -= 1;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: Scaffold(
        body: Stack(
          fit: StackFit.expand,
          children: <Widget>[
            Image.asset("images/$_date.png", fit: BoxFit.fill),
            Positioned(
                top: 230,
                left: 360,
                child: AnimatedTextKit(
                  animatedTexts: [
                    TyperAnimatedText(
                      _poem[_date - 1][0],
                      textStyle: TextStyle(fontSize: 22),
                      speed: const Duration(milliseconds: 200),
                    ),
                  ],
                  isRepeatingAnimation: false,
                )),
            // Positioned(
            //     top: 100,
            //     left: 320,
            //     child: AnimatedTextKit(
            //       animatedTexts: [
            //         TyperAnimatedText(
            //           _poem[_date - 1][1],
            //           textStyle: TextStyle(fontSize: 22),
            //           speed: const Duration(milliseconds: 200),
            //         ),
            //       ],
            //       isRepeatingAnimation: false,
            //     )),
            // Positioned(
            //     top: 100,
            //     left: 270,
            //     child: AnimatedTextKit(
            //       animatedTexts: [
            //         TyperAnimatedText(
            //           _poem[_date - 1][2],
            //           textStyle: TextStyle(fontSize: 22),
            //           speed: const Duration(milliseconds: 200),
            //         ),
            //       ],
            //       isRepeatingAnimation: false,
            //     )),
            Positioned(
              top: 30,
              right: 30,
              child: InkWell(
                onTap: () {
                  ///点击跳过，在此处可以写跳转
                  print("点击跳过，在此处可以写跳转代码，记得销毁界面哦");
                },
                child: Container(
                  padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                  height: 32,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Color.fromARGB(10, 255, 255, 255),
                  ),
                  child: RichText(
                    text: TextSpan(children: <TextSpan>[
                      TextSpan(
                          text: '$_countdown',
                          style: TextStyle(
                            fontSize: 18,
                            color: Colors.blue,
                          )),
                      TextSpan(
                          text: '跳过',
                          style: TextStyle(
                            fontSize: 18,
                            color: Colors.red,
                          )),
                    ]),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
